<template>
	<view class="uni-numbox">
		<view class="uni-numbox-minus" @click="handleReduce">-</view>
		<input class="uni-numbox-value" type="number" v-model="currentValue" @change="handleChange" disabled>
		<view class="uni-numbox-plus" @click="handleIncrease">+</view>
	</view>
</template>

<script>
	export default {
		props: {
			value: {
				type: Number,
				default: 0
			},
		},
		data() {
			return {
				currentValue: this.value,
			}
		},

		watch: {
			currentValue(val) {
				this.$emit('input', val);
				this.$emit('on-change', val);
			},
			value(val) {
				this.updateValue(val);
			}
		},
		methods: {
			updateValue(val) {
				this.currentValue = val;
			},
			handleChange(event) {
				//允许进一步判断输入的是否是数字
				// event.target.value = this.currentValue;
			},
			handleIncrease() {
				this.$emit('increase');
				this.currentValue++;
			},
			handleReduce() {
				this.$emit('reduce');
				//最少得留一件
				if (this.currentValue <= 1) return;

				this.currentValue--;
			}
		},
		mounted() {
			this.updateValue(this.value);
		}
	}
</script>

<style>
	.uni-numbox {
		display: flex;
		flex-direction: row;
		height: 50px;
	}

	.uni-numbox-minus,
	.uni-numbox-plus {
		margin: 0;
		background-color: #f9f9f9;
		width: 50px;
		line-height: 50px;
		justify-content: center;
		color: #555555;
		font-size: 24px;
		border: 0px;
	}

	.uni-numbox-value {
		border: 0px;
		width: 100px;
		font-size: 24px;
		text-align: center;
	}
</style>